<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0">
    <title>Register</title>
    <script src="Scripts/jquery-1.5.1.js" type="text/javascript"></script>
  <script type="text/javascript" language="JavaScript">


function initDatabase() {//初始化数据库
      var db = getCurrentDb();
      if(!db) {
          alert("您的浏览器不支持HTML5");
          return;
      }
      

      db.transaction(function (trans) {
          trans.executeSql("create table if not exists Register(userName text null,userEmail text null,userPassword text null)", [], function (trans, result) {
              
          }, function (trans, message) {
              alert(message);
          });
      }, function (trans, result) {
      }, function (trans, message) {
      });
  }

  $(function () {

      initDatabase();

      $("#btnSubmit").click(function () {

        var returnValue = true;
          //姓名不少于两个字
          var username = Name.value;
          if(username.length < 2) {
            returnValue = false;
            alert("姓名不少于两个字。");
            Name.value="";
            Name.focus();
            return false;
            //return false 才能不刷新页面
          }

          //   //E-mail符合电子邮箱的正则表达式

          var emailEnterd=Email.value;
          var regex = /^([\w-_]+(?:\.[\w-_]+)*)@((?:[a-z0-9]+(?:-[a-zA-Z0-9]+)*)+\.[a-z]{2,6})$/i ;
          console.log(document.form);
          if (!regex.test( emailEnterd ) )
          {
           alert( "您输入的电子邮件地址不合法,请重新输入。" );
           Email.value="";
           Email.focus();
           returnValue= false;
           return false;
          }

          //密码
          var password1 =Password.value;
          if(password1.length < 6) {
            alert("密码不少于6位。");
            Password.value = "";
           PasswordAgain.value = "";
            Password.focus();
            returnValue = false;
            return false;
          }


          

          //再次输入密码
          var password2 = PasswordAgain.value;
          if(password2 != password1){
            alert("密码不匹配，请再次输入密码。");
            PasswordAgain.value = "";
            PasswordAgain.focus();
            returnValue = false;
            return false;
          }


          var txtName = $("#Name").val();
          var txtEmail = $("#Email").val();
          var txtPassword = $("#Password").val();

          var db = getCurrentDb();


          db.transaction(function (trans) {
               trans.executeSql("select * from Register where  userName=?", [txtName], function (ts, data) {
                    if (data) {
                           for (var i = 0; i < data.rows.length; i++) {
                                var row =data.rows.item(i);//
                                if(row.userPassword){
                                  alert("该用户名已被注册，请重新注册！");
                                  window.location.href="register-weui.html";
                                  return;
                                }
                          }
                               trans.executeSql("insert into Register(userName,userEmail,userPassword) values(?,?,?) ", [txtName,txtEmail, txtPassword], function (ts, data) {
                                }, function (ts, message) {
                                    alert(message);
                                });
                               alert("提交成功!去登陆。");
                                window.location.href="login-weui.html";
                       }         
                      
                }, function (ts, message) {
                  alert("true");
                    alert(message);
                    var tst = message;
                });


            });
      });
  });

  function getCurrentDb() {
      //打开数据库，或者直接连接数据库参数：数据库名称，版本，概述，大小
      var db = openDatabase("myDb", "1.0", "it's to save register data!", 1024 * 1024); ;
      return db;
       
  }

function passStrength(form) {
  //密码
    var password1 = Password.value;
    var strongRegex = new RegExp("^(?=.{8,})(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*\W).*$", "g");
    var mediumRegex = new RegExp("^(?=.{7,})(((?=.*[A-Z])(?=.*[a-z]))|((?=.*[A-Z])(?=.*[0-9]))|((?=.*[a-z])(?=.*[0-9]))).*$", "g");
    if(password1.length >= 6) {
        if (strongRegex.test(password1))
        {
            var ps=document.getElementById("passstrength1");  
            ps.innerHTML='强!';  
        }
        else if (mediumRegex.test(password1))
        {
            var ps=document.getElementById("passstrength1");  
            ps.innerHTML='中!';  
        }
        else
        {
            var ps=document.getElementById("passstrength1");  
            ps.innerHTML='弱!';  

        }
    }
}

  </script>
    <link rel="stylesheet" href="weui.min.css">
    <link rel="stylesheet" href="weui.css">

</head>
<body>

<div class="page" style="text-align:center;">
        <div class="page__hd">
        <h1 class="page__title">
            <img src="icon.jpg" height="180px" />
        </h1>
    </div>

        <div class="weui-cells__title">注册</div>
        <div class="weui-cells weui-cells_form" >
            <div class="weui-cell" style="left:40%">
                <div class="weui-cell__hd"><label class="weui-label">姓名</label></div>
                <div class="weui-cell__bd">
                    <input class="weui-input" class="required" name="Name" id="Name" size="20" placeholder="请输入姓名"/>
                </div>
            </div>
            <div class="weui-cells  weui-cells_form">
            <div class="weui-cell" style="left:40%">
                <div class="weui-cell__hd">
                    <label class="weui-label">邮箱</label>
                </div>
                <div class="weui-cell__bd">
                    <input class="weui-input" name="Email" id="Email"  placeholder="请输入邮箱"/>
                </div>
                </div>
            </div>
            <div class="weui-cells  weui-cells_form">
            <div class="weui-cell" style="left:40%">
                <div class="weui-cell__hd">
                    <label class="weui-label">密码</label>
                </div>
                <div class="weui-cell__bd">
                    <input class="weui-input" type="password"  name="Password" id="Password" onkeyup="return passStrength(form);" placeholder="请输入密码"/>
                </div>
             </div>   
            </div>
            
            <div class="weui-cell" style="left:40%">
            <div class="weui-cell__hd">
                    <label class="weui-label">密码强度</label>
                </div>
                <div class="weui-cell__bd">
                    <label class="weui-label" id="passstrength1"></label>
                </div>
           
                </div>
              </div>  
            

            <div class="weui-cells  weui-cells_form">
            <div class="weui-cell" style="left:40%">
                <div class="weui-cell__hd">
                    <label class="weui-label">密码</label>
                </div>
                <div class="weui-cell__bd">
                    <input class="weui-input" type="password" name="PasswordAgain" id="PasswordAgain" placeholder="请再次输入密码确认"/>
                </div>
              </div>  
            </div>
        </div>


        <div class="weui-btn-area">
            <a class="weui-btn weui-btn_primary" style="width:30%" name="btnSubmit" id="btnSubmit">提交</a>
        </div>
    </div>

</div>

</body>
</html>



